//
//  OL_SearchViewController.m
//  YUXiu
//
//  Created by 王竞成 on 16/6/24.
//  Copyright © 2016年 Wang. All rights reserved.
//

#import "OL_SearchViewController.h"
#import "CircleView.h"
#import "CourseCell.h"
#import "MediaListCell.h"

@interface OL_SearchViewController ()<UISearchBarDelegate,UIScrollViewDelegate>{
    NSString *seText;
    UIView *firstView;
    NSArray *dataArray;
    BOOL isHid;
    UIButton * leftItem;
    
    UIScrollView *mainScrollV;
    
    NSString *type;
}

@property (nonatomic ,strong)UISearchBar *searchBar;    //导航搜索
@property (nonatomic,strong)UITableView *table1;
@property (nonatomic,strong)UITableView *table2;

@end

@implementation OL_SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    type = @"教练";
}
#pragma mark - UI
-(void)autoConfigViews{
    //创建searchBar
    _searchBar = [[UISearchBar alloc]initWithFrame:krect(5, 24, kScreenWidth-5, 40)];
    _searchBar.barTintColor = kNaviBarCOLOR;
    [_searchBar setTintColor:kOrangeCOLOR];
    _searchBar.delegate = self;
    _searchBar.placeholder = @"请输入关键字";
    _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;//不自动大写
    _searchBar.translucent  = YES;

    [_searchBar setShowsCancelButton:YES];
    [_searchBar setShowsBookmarkButton:YES ];
    [_searchBar becomeFirstResponder];
    [_searchBar setImage:[UIImage imageNamed:@"header"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
    
    _searchBar.autocorrectionType = UITextAutocorrectionTypeNo;//不自动纠错
    CALayer *layer=[_searchBar layer];
    //是否设置边框以及是否可见
    [layer setMasksToBounds:NO];
    //设置边框圆角的弧度
    [layer setCornerRadius:0.0];
    //设置边框线的宽
    [layer setBorderWidth:1];
    //设置边框线的颜色
    [layer setBorderColor:kNaviBarCOLOR.CGColor];
    //光标颜色
    [[[_searchBar.subviews objectAtIndex:0].subviews objectAtIndex:1] setTintColor:kOrangeCOLOR];

    for(id cc in [_searchBar.subviews[0] subviews])
    {
        if([cc isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cc;
            [btn setTitle:@"取消"  forState:UIControlStateNormal];
            [btn setTitleColor:kOrangeCOLOR forState:UIControlStateNormal];
        }
    }

    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.view addSubview:_searchBar];
   

    mainScrollV = [[UIScrollView alloc]initWithFrame:krect(0, 0, kScreenWidth*2, kScreenHeight)];
    mainScrollV.contentSize = CGSizeMake(kScreenWidth*3, kScreenHeight);
    mainScrollV.delegate = self;
    mainScrollV.backgroundColor = kWhiteCOLOR;
    mainScrollV.scrollEnabled = NO;
    
    _table1 = [[UITableView alloc]initWithFrame:krect(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _table1.delegate = self;
    _table1.dataSource = self;
    _table1.separatorStyle = UITableViewCellSeparatorStyleNone;

    _table2 = [[UITableView alloc]initWithFrame:krect(kScreenWidth, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    _table2.delegate = self;
    _table2.dataSource = self;
    _table2.separatorStyle = UITableViewCellSeparatorStyleNone;

    [mainScrollV addSubview:_table2];
    [mainScrollV addSubview:_table1];
    
    [self.view addSubview:mainScrollV];
    [self firstView];

    
}
-(void)firstView{
    NSArray *titleArr = @[@"直播",@"课程",@"教练"];
    firstView = [[UIView alloc]initWithFrame:krect(0, 40, kScreenWidth, 100)];
    for (int i = 0 ; i<3; i++) {
        CircleView *cirV = [[CircleView alloc]initWithFrame:krect(kScreenWidth/3 * i, 0, kScreenWidth/3, 100)];
        cirV.str = titleArr[i];
        cirV.block = ^(NSString *str){
            NSLog(@"点击了哪个头像:%@",str);
            type = str;
            [UIView animateWithDuration:0.3 animations:^{
                [mainScrollV setFrame:krect(-kScreenWidth, 0, 2*kScreenWidth, kScreenHeight)];
                
            }];
            [_searchBar setFrame:krect(35, 24, kScreenWidth-40, 40)];
            _searchBar.placeholder = [NSString stringWithFormat:@"搜索%@",str];

                leftItem = [[UIButton alloc]initWithFrame:krect(-5, 30, 30, 20)];
                [leftItem addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
                leftItem.tag = 10;
            //inform_nav_icn_return_normal
                [leftItem setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
                [self.navigationController.view addSubview:leftItem];
            WS(ws);
            [leftItem mas_makeConstraints:^(MASConstraintMaker *make) {
                //
                make.right.equalTo(ws.searchBar.mas_left).with.offset(0);
                make.centerY.equalTo(ws.searchBar);
            }];
            
            [_table2 reloadData];
        };
        [firstView addSubview:cirV];
    }
    [mainScrollV addSubview:firstView];
    [_table1 setHidden:YES];
    isHid = firstView.hidden;
}
//
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"打开历史记录");
    if (!isHid) {
        [firstView setHidden:YES];
        dataArray = @[@"中国最美女教练",@"完美身材尼马",@"直播深圳"];
        [_table1 reloadData];
        [_table1 setHidden:NO];


    }else{
        [firstView setHidden:NO];
        self.view.backgroundColor = kWhiteCOLOR;
        [_table1 setHidden:YES];
    }
    isHid = !isHid;
    //刷新tableview,section "历史记录"
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    //参照微信，导航栏返回按钮
    [leftItem setHidden:YES];

    [self.navigationController popViewControllerAnimated:NO];
    
}
-(void)click:(UIButton *)sender{
//    tag = 10,navi left
    if (sender.tag==10) {
        [UIView animateWithDuration:0.3 animations:^{
            [mainScrollV setFrame:krect(0, 0, 2*kScreenWidth, kScreenHeight)];
        }];
        [_searchBar setFrame:krect(5, 24, kScreenWidth-5, 40)];
        _searchBar.placeholder = @"请输入关键字";

    }
    

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_searchBar resignFirstResponder];
    _searchBar.hidden = YES;
}
#pragma mark - tableview delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _table1) {
        return dataArray.count;

    }else{
        return 3;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _table1) {
        return 60;
    }
    if (tableView == _table2) {
        if ([type isEqualToString:@"教练"]) {
            return 60;
        }else if([type isEqualToString:@"课程"]){
            return 110;
        }else{
            return 110;
        }
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _table1) {
        static NSString *iden = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
            cell.backgroundColor = kNaviBarCOLOR;
        }
        UIView *line = [[UIView alloc]initWithFrame:krect(2, 59, kScreenWidth-5, 1)];
        line.backgroundColor = kLightGrayCOLOR;
        [cell.contentView addSubview:line];
        
        UIImageView *headimg = [[UIImageView alloc]initWithFrame:krect(10, 15, 30, 30)];
        [headimg setImage:[UIImage imageNamed:@"header"]];
        [cell.contentView addSubview:headimg];
        UILabel *titleL = [[UILabel alloc]initWithFrame:krect(headimg.golf_right+3, 20, kScreenWidth-50, 20)];
        titleL.text = dataArray[indexPath.row];
        [cell.contentView addSubview:titleL];
        return cell;

    }else{//table2
        if ([type isEqualToString:@"教练"]) {
            CourseCell *cell = [[CourseCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"adcell"];

            return cell;
        }else if ([type isEqualToString:@"课程"]){
            MediaListCell *cell = [[MediaListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell3"];
            
            return cell;
        }else{//直播
            MediaListCell *cell = [[MediaListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell3"];
            
            return cell;
        }
       

    }
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == _table1) {
        UIView *hdView = [[UIView alloc]initWithFrame:krect(0, 0, kScreenWidth, 25)];
        hdView.backgroundColor = kNaviBarCOLOR;
        UILabel *title = [[UILabel alloc]initWithFrame:krect(10, 0, kScreenWidth-10, 24)];
        title.textAlignment = NSTextAlignmentLeft;
        title.text = @"搜索历史";
        title.textColor = kLightGrayCOLOR;
        
        UIView *line = [[UIView alloc]initWithFrame:krect(10, title.golf_bottom, kScreenWidth-20, 1)];
        line.backgroundColor = kLightGrayCOLOR;
        
        [hdView addSubview:line];
        [hdView addSubview:title];
        return hdView;

    }else{
        return nil;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (tableView == _table1) {
        return 25;
    }else{
        return 0;
    }
}
#pragma mark  --  SearchBar Delegate
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    seText = searchText;
    UIButton *btn = (UIButton *)[self.navigationController.view viewWithTag:10];
    [btn setHidden:YES];
    
    //请求数据,
    if (searchText.length>0) {
        [firstView setHidden:YES];
    }else{
        [firstView setHidden:NO];
    }
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
//    for(id cc in [searchBar.subviews[0] subviews])
//    {
//        if([cc isKindOfClass:[UIButton class]])
//        {
//            UIButton *btn = (UIButton *)cc;
//            [btn setTitle:@"取消"  forState:UIControlStateNormal];
//            [btn setTitleColor:kOrangeCOLOR forState:UIControlStateNormal];
//        }
//    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
